package com.chenyue.cm.api.homePage;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.vo.Page;
import com.chenyue.cm.news.service.JdNewsService;
import com.chenyue.cm.news.vo.NewInfoVo;
import com.chenyue.cm.station.domain.JdStation;
import com.chenyue.cm.station.service.JdStationService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: ljy
 * @Title: HomePageController
 * @Description: 首页Api
 * @Date: 2021-07-21
 */
@RestController
@RequestMapping("/api/homePage")
@Api(value = "首页信息api", tags = "首页信息api")
@ApiSort(2)
public class HomePageController {

    private final JdNewsService newsService;
    private final JdStationService jdStationService;

    public HomePageController(
            JdNewsService newsService,
            JdStationService jdStationService
    ) {
        this.newsService = newsService;
        this.jdStationService = jdStationService;
    }

    /**
     * @return Success
     */
    @GetMapping("/getHomeNewsList")
    @ApiOperation(value = "首页新闻列表", notes = "首页新闻列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "page", dataType = "int", required = true),
            @ApiImplicitParam(value = "条数", name = "rows", dataType = "int", required = true),
    })
    public Success<Page<List<NewInfoVo>>> getHomeNewsList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows
    ) {
        Page<List<NewInfoVo>> homeNewsList = newsService.getAllNewsList(page, rows);
        return Success.success(homeNewsList);
    }

    /**
     * @param type 1球馆2健身房
     * @return Success
     */
    @PostMapping("/getHomeStationList")
    @ApiOperation("首页场馆列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "page", dataType = "int", required = true),
            @ApiImplicitParam(value = "条数", name = "rows", dataType = "int", required = true),
            @ApiImplicitParam(value = "场馆类型", name = "type", dataType = "int"),
    })
    public Success<Page<List<JdStation>>> getHomeStationList(
            Integer type,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows
    ) {
        final LambdaQueryChainWrapper<JdStation> eq = jdStationService.lambdaQuery();
        if (type != null && type > 0) {
            eq.eq(JdStation::getType, type);
        }
        int count = eq.count();
        final Page<List<JdStation>> result = new Page<>(page, rows, count);
        final List<JdStation> list = eq.last(result.convertPage()).list();
        result.setContent(list);
        return Success.success(result);
    }
}
